package com.midian.controller.activity;

import java.net.URLDecoder;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.midian.common.exception.BizException;
import com.midian.common.filter.annotation.Validate;
import com.midian.common.pager.Pager;
import com.midian.common.pay.common.util.PayUtils;
import com.midian.common.pay.common.util.iorder.OpOrder;
import com.midian.common.system.ErrorConstants;
import com.midian.common.system.RetManager;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.ContentJSONObject;
import com.midian.common.utils.DateUtils;
import com.midian.common.utils.ObjUtils;
import com.midian.common.utils.PicUtils;
import com.midian.common.utils.StringUtil;
import com.midian.model.activity.Activity;
import com.midian.model.activity.ActivityComment;
import com.midian.model.activity.ActivityCommentPic;
import com.midian.model.activity.ActivityType;
import com.midian.model.activity.ActivityUser;
import com.midian.model.banner.Banner;
import com.midian.model.pic.Pic;
import com.midian.model.sys.SysConf;
import com.midian.model.user.User;
import com.midian.service.activity.IActivityCommentPicService;
import com.midian.service.activity.IActivityCommentService;
import com.midian.service.activity.IActivityPicService;
import com.midian.service.activity.IActivityService;
import com.midian.service.activity.IActivityTypeService;
import com.midian.service.activity.IActivityUserService;
import com.midian.service.banner.IBannerService;

/**
 * 活动控制器
 * 
 */
@Controller("activityController")
public class ActivityController {
	/** 日志 */
	private static Logger LOG = Logger.getLogger(ActivityController.class);
	/** 反馈意见业务 */
	private IActivityCommentPicService activityCommentPicService;
	private IActivityCommentService activityCommentService;
	private IActivityPicService activityPicService;
	private IActivityService activityService;
	private IActivityTypeService activityTypeService;
	private IActivityUserService activityUserService;
	private IBannerService bannerService;
	
	
	@Resource(name = "bannerService")
	public void setBannerService(IBannerService bannerService) {
		this.bannerService = bannerService;
	}

	@Resource(name = "activityCommentPicService")
	public void setActivityCommentPicService(IActivityCommentPicService activityCommentPicService) {
		this.activityCommentPicService = activityCommentPicService;
	}



	@Resource(name = "activityCommentService")
	public void setActivityCommentService(IActivityCommentService activityCommentService) {
		this.activityCommentService = activityCommentService;
	}

	@Resource(name = "activityPicService")
	public void setActivityPicService(IActivityPicService activityPicService) {
		this.activityPicService = activityPicService;
	}

	@Resource(name = "activityService")
	public void setActivityService(IActivityService activityService) {
		this.activityService = activityService;
	}

	@Resource(name = "activityTypeService")
	public void setActivityTypeService(IActivityTypeService activityTypeService) {
		this.activityTypeService = activityTypeService;
	}

	@Resource(name = "activityUserService")
	public void setActivityUserService(IActivityUserService activityUserService) {
		this.activityUserService = activityUserService;
	}

	PicUtils picUtils;

	@Resource(name = "picUtils")
	public void setPicUtils(PicUtils picUtils) {
		this.picUtils = picUtils;
	}

	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/activities", method = RequestMethod.GET)
	public @ResponseBody
	Object activities(String client_key, @RequestParam(required = false) String page, // 页码
			@RequestParam(required = false) String size, // 页大小
			@RequestParam(required = false) String lon,// 经度
			@RequestParam(required = false) String lat,// 纬度
			@RequestParam(required = false) String time_filter,// 单选,today:今天；tomorrow:明天；weekend:周末；
			@RequestParam(required = false) String type_filter,// 多选，类型ids，多个用逗号隔开，不传表示不限类型
			@RequestParam(required = false) String age_filter, // 多选，多个用逗号隔开，不传表示不限年龄
			@RequestParam(required = false) String distance_filter,// 单选，不传表示不限距离，其值为数字，单位为km
			@RequestParam(required = false) String key)// 搜索关键字，匹配活动标题
			throws Exception {
		String str = null; 
		if(key != null){
			str = new String(key.getBytes("iso-8859-1"), "utf-8");
		}
		JSONObject json = new JSONObject();
		try {
			Pager pager = new Pager();
			if (StringUtils.isNotBlank(page)) {
				pager.setCurrentPage(Integer.parseInt(page));
			}
			if (StringUtils.isNotBlank(size)) {
				pager.setPageSize(Integer.parseInt(size));
			}
			
			List<Activity> activitiesAfterToday = activityService.getActivities(lon, lat, time_filter, type_filter, age_filter, distance_filter, str, pager);
			JSONArray jsonArray = new JSONArray();
			for (Activity activity : activitiesAfterToday) {
				ContentJSONObject jsonObject = new ContentJSONObject();
				jsonObject.put("activity_id", activity.getId());
				jsonObject.put("cover_pic", activity.getCoverPicId());
				jsonObject.put("title", activity.getTitle());
				jsonObject.put("begin_time", DateUtils.fotmatDate11(activity.getBeginTime()));
				jsonObject.put("type_name", activity.getTypeName());
				jsonObject.put("organization_name", activity.getOrganizationName());
				jsonObject.put("adult_price", activity.getAdultPrice());
				jsonObject.put("child_price", activity.getChildPrice());
				jsonObject.put("ages", activity.getAges());
				jsonObject.put("activity_address", activity.getActivityAddress());
				jsonObject.put("activity_address_lon", activity.getActivityAddressLon());
				jsonObject.put("activity_address_lat", activity.getActivityAddressLat());
				jsonObject.put("distance", activity.getDistance() == null ? "" : activity.getDistance() / 1000 + "km");
				jsonArray.add(jsonObject.getContent());
			}
			json.put("content", jsonArray);
			
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

	
	
	
	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/activity_detail", method = RequestMethod.GET)
	public @ResponseBody
	Object activityDetail(String client_key, String activity_id, @RequestParam(required = false) String user_id, @RequestParam(required = false) String access_token,
			@RequestParam(required = false) String lon,// 经度
			@RequestParam(required = false) String lat// 纬度
	) throws Exception {
		JSONObject json = new JSONObject();
		try {
			Activity activity = activityService.getDetail(activity_id, lon, lat);
			ContentJSONObject contentObject = new ContentJSONObject();
			contentObject.put("activity_id", activity.getId());
			contentObject.put("cover_pic", activity.getCoverPicId());
			contentObject.put("title", activity.getTitle());
			contentObject.put("begin_time", activity.getBeginTime());
			contentObject.put("type_name", activity.getTypeName());
			contentObject.put("organization_name", activity.getOrganizationName());
			contentObject.put("adult_price", activity.getAdultPrice());
			contentObject.put("child_price", activity.getChildPrice());
			contentObject.put("ages", activity.getAges());
			contentObject.put("activity_address", activity.getActivityAddress());
			contentObject.put("activity_address_lon", activity.getActivityAddressLon());
			contentObject.put("activity_address_lat", activity.getActivityAddressLat());
			contentObject.put("contact_address", activity.getContactAddress());
			contentObject.put("contact_address_lon", activity.getContactAddressLon());
			contentObject.put("contact_address_lat", activity.getContactAddressLat());
			contentObject.put("contact_person", activity.getContactPerson());
			contentObject.put("contact_num", activity.getContactNum());
			contentObject.put("join_limit_count", activity.getJoinLimitCount());
			contentObject.put("join_count", activity.getJoinCount());
			contentObject.put("is_adult_record", activity.getIsAdultRecord());
			contentObject.put("distance", activity.getDistance() == null ? "" : activity.getDistance() / 1000 + "km");
			contentObject.put("detail", "activity_detail_page/" + activity.getId());
			contentObject.put("share_url", "activity_share_page/" + activity.getId());
			JSONArray pics = new JSONArray();
			String picsStr = activity.getPicIds();
			if (StringUtils.isNotBlank(picsStr)) {
				String[] picsArray = picsStr.split(",");
				for (String pic : picsArray) {
					pics.add(pic);
				}
			}
			contentObject.put("pics", pics);

			// 是否参加
			if (activityUserService.getActivityUser(activity_id, user_id,1) == null) {
				contentObject.put("is_join", "2");
			} else {
				contentObject.put("is_join", "1");
			}
			json.put("content", contentObject.getContent());

		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

	@RequestMapping(value = "/activity_share_page/{id}", method = RequestMethod.GET)
	public String getActivitySharePage(@PathVariable("id") String id,Model model) {
		ModelAndView mav = new ModelAndView();
		try {
			Activity activity = activityService.get(id);
			model.addAttribute("detail", activity == null ? null : activity.getDetail());
		} catch (BizException e) {
			LOG.error(e.getMessage());
		}
		return "activityShare";
	}
	
	@RequestMapping(value = "/activity_detail_page/{id}", method = RequestMethod.GET)
	public String getProductShowPage(@PathVariable("id") String id,Model model) {
		ModelAndView mav = new ModelAndView();
		try {
			Activity activity = activityService.get(id);
			model.addAttribute("detail", activity == null ? null : activity.getDetail());
		} catch (BizException e) {
			LOG.error(e.getMessage());
		}
		return "activityDetail";
	}

	@Validate(retCode = RetManager.private_enroll_error)
	@RequestMapping(value = "/join_activity", method = RequestMethod.POST)
	public @ResponseBody
	Object joinActivity(String client_key, String user_id, String access_token, String activity_id, String adult_count, String child_count, String contact_person, String contact_num,
			@RequestParam(required = false) String pay_way, @RequestParam(required = false) String remark, HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject json = new JSONObject();
		try {
			
			//判断是否同一个活动用户报名信息已存在
			ActivityUser judgeUserIfExist = activityUserService.getActivityUser(activity_id, user_id, 1);
			if (judgeUserIfExist!=null) {
				RetManager.getInstance().setRetError(json, RetManager.private_no_repeat_enroll);
				return json;
			}
			Activity activity = activityService.get(activity_id);
			if (activity == null) {
				RetManager.getInstance().setRetError(json, RetManager.private_activity_not_exist);
				return json;
			} else {
				// 报名人数超出限制
				if (Integer.parseInt(child_count) + Integer.parseInt(adult_count) + activity.getJoinCount() > activity.getJoinLimitCount()) {
					RetManager.getInstance().setRetError(json, RetManager.private_enroll_person_count_too_many);
					return json;
				}

				Integer adultCount = Integer.parseInt(adult_count);
				Integer childCount = Integer.parseInt(child_count);
				final ActivityUser activityUser = new ActivityUser();
				activityUser.setOrderSn(StringUtil.generateOrderSn());
				activityUser.setActivityId(activity.getId());
				activityUser.setCoverPicId(activity.getCoverPicId());
				activityUser.setTitle(activity.getTitle());
				activityUser.setBeginTime(activity.getBeginTime());
				activityUser.setJoinUserId(user_id);
				activityUser.setAdultCount(adultCount);
				activityUser.setChildCount(childCount);
				activityUser.setAdultPrice(activity.getAdultPrice());
				activityUser.setChildPrice(activity.getChildPrice());
				activityUser.setContactPerson(contact_person);
				activityUser.setContactNum(contact_num);
				activityUser.setRemark(remark);

				ContentJSONObject contentObject = new ContentJSONObject();
				contentObject.put("order_sn", activityUser.getOrderSn());

				ContentJSONObject payObject = new ContentJSONObject();
				if (activity.getAdultPrice() == 0 && activity.getChildPrice() == 0) {
					// 免费设为已支付
					activityUser.setJoinState(1);
					activityUser.setPayWay("");
					contentObject.put("pay_way", "");// 免费设置支付方式为空
					
					//获取已经参加人数用activityuser中的大人人数和小孩人数做累加
					activity.setJoinCount(activity.getJoinCount()+activityUser.getChildCount()+activityUser.getAdultCount());
				} else {
					activityUser.setJoinState(2);
					contentObject.put("pay_way", pay_way);
					Double payPrice = adultCount * activity.getAdultPrice() + childCount * activity.getChildPrice();
					if ("weixin".equals(pay_way)) {
						activityUser.setPayWay(pay_way);
						JSONObject weixinJson = PayUtils.getWeiXinJSON(request, response, activityUser.getOrderSn(), payPrice, new Date(), activityUser.getTitle());
						payObject.put("appid", weixinJson.get("appid"));
						payObject.put("noncestr", weixinJson.get("noncestr"));
						payObject.put("pkg",weixinJson.get("package"));
						payObject.put("partnerid", weixinJson.get("partnerid"));
						payObject.put("prepayid", weixinJson.get("prepayid"));
						payObject.put("sign", weixinJson.get("sign"));
						payObject.put("timestamp", weixinJson.get("timestamp"));
						
//						//获取已经参加人数用activityuser中的大人人数和小孩人数做累加
//						activity.setJoinCount(activity.getJoinCount()+activityUser.getChildCount()+activityUser.getAdultCount());
						contentObject.put("weixin_param", payObject);
					} else if ("alipay".equals(pay_way)) {
						activityUser.setPayWay(pay_way);
						payObject.put("pay_price", payPrice);
						payObject.put("pay_title", activity.getTitle());
						payObject.put("pay_desc", "活动报名费用");
						payObject.put("callback_url", "pay_ali_callback");
						payObject.put("merchant_no", "2088421517531188");
						payObject.put("receive_account", "3512715657@qq.com");
						payObject.put("private_key", "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMaSHnyp4On74H9u7l/z2tvwCwQFylarQ7I2RPam/64OBi7+xiUMgeLpeX65gWA4aeG2xnz9KtP1L6bCqxlrf5RE3ZJWUOu+yb2EGWxkCMrbzpYPJc/+4X3LSYLmt5+7QihTVHRh/XkOKBsLWNu1R9fma0ns2+vdu7VrmM4wGLO5AgMBAAECgYBH2qojjElyAg4LrvWdTR2eRq+Fxh6g3BqEC3VLF8Ufk72TFehXkb4SgU/QlooFaoSOA9ixc98ike+rVXgam7avKj9k9YCRUM/hH4v+21TwQ5bPxBnLwFsWK9EmTkSwPYrDNnK9+3Ebs4FI0qTsqs3LoZ+/8HbsgZS6Y00Pe4/PsQJBAOcFc9mapWeSlfmn3UmGr0AduaA3QccGhSUNIZJNzwsO/PpXlCcTi5lbooolp/Mbf6s9sB3ay67s3yR+pjqumCUCQQDcCnQJmczGlXhFWvB9v199xwpo2SlnRKLtrvzjz/CYazVEC9f93i5kN8zpEiJZbP2HPAyOPJsOI+PYuE2VMl8FAkAdopZs6KilM6mngAAkCXH7wJRqWAxJEq4LxbZzxSV1HrQXDRsFND2pK5cbL59u0S08c0pfWgzc4KfzT4fVaUY9AkEAzLGUvJGso1fMqIgujGwIwxL0JhFOqNh60K0maC66XK/uvzxdU7bqZSRbpL3P4my3dKRx8aeDfqX4gVUCJzbSmQJAF3+Z+D7Gu4E1wj/fiq09i7AzQNB8Vcc+cqHMZo11usRVIV5Xyz3IuUZuVxeseAbTc7Wh6n5nXNhCXuZVjwZ6qw==");
						
//						//获取已经参加人数用activityuser中的大人人数和小孩人数做累加		
//						activity.setJoinCount(activity.getJoinCount()+activityUser.getChildCount()+activityUser.getAdultCount());
						contentObject.put("alipay_param", payObject);
					} else {
						// 支付方式不支持
						RetManager.getInstance().setRetError(json, RetManager.private_payway_no_support);
						return json;
					}
				}
				// TODO ===leeib=== 支付待处理
				json.put("content", contentObject.getContent());
				activityService.update(activity);
				ActivityUser tageTactivityUser = activityUserService.add(activityUser);

				if (tageTactivityUser != null) {
					RetManager.getInstance().setRetSuccess(json, RetManager.private_enroll_success);
				} else {
					RetManager.getInstance().setRetError(json, RetManager.private_enroll_fail);
				}
			}

		} catch (Exception e) {
			LOG.error("message:::"+e.getMessage()+",StackTrace:::"+e.getStackTrace());
			throw e;
		}
		return json;
	}

	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/activity_comments", method = RequestMethod.GET)
	public @ResponseBody
	Object activityComments(String client_key, String activity_id, String page, String size) throws Exception {
		JSONObject json = new JSONObject();
		try {
			Pager pager = new Pager();
			if (StringUtils.isNotBlank(page)) {
				pager.setCurrentPage(Integer.parseInt(page));
			}
			if (StringUtils.isNotBlank(size)) {
				pager.setPageSize(Integer.parseInt(size));
			}
			List<ActivityComment> activityComments = activityCommentService.getComments(activity_id, pager);
			JSONArray contentArray = new JSONArray();
			if (!activityComments.isEmpty()) {
				for (ActivityComment activityComment : activityComments) {
					ContentJSONObject jsonObject = new ContentJSONObject();
					jsonObject.put("comment_id", activityComment.getId());
					jsonObject.put("head_pic", activityComment.getUser() == null ? "" : activityComment.getUser().getHeadPicId());
					jsonObject.put("name", activityComment.getUser().getName());
					jsonObject.put("time", activityComment.getCreateTime());
					jsonObject.put("content", activityComment.getContent());

					JSONArray picArray = new JSONArray();
					Set<ActivityCommentPic> activityCommentPics = activityComment.getPics();
					if (!activityCommentPics.isEmpty()) {
						for (ActivityCommentPic activityCommentPic : activityCommentPics) {
							picArray.add(activityCommentPic.getPicId());
						}
					}
					jsonObject.put("pics", picArray);
					contentArray.add(jsonObject.getContent());
				}
			}
			json.put("content", contentArray);
			json.put("comment_count", String.valueOf(activityCommentService.getCommentCount(activity_id)));
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

	@Validate(retCode = RetManager.common_comment_error)
	@RequestMapping(value = "/add_activity_comment", method = RequestMethod.POST)
	public @ResponseBody
	Object addActivityComment(String client_key, String activity_id, String user_id, String access_token, String content, @RequestParam(required = false) MultipartFile[] pics) throws Exception {
		JSONObject json = new JSONObject();
		try {
			ActivityComment activityComment = new ActivityComment();
			activityComment.setActivityId(activity_id);
			activityComment.setContent(content);
			activityComment.setUserId(user_id);

			if (pics != null && pics.length > 0) {
				Set<ActivityCommentPic> picSet = new HashSet<ActivityCommentPic>();
				for (int i = 0; i < pics.length; i++) {
					try {
						MultipartFile multipartFile = pics[i];
						Pic pic = picUtils.uploadPic(multipartFile);
						ActivityCommentPic activityCommentPic = new ActivityCommentPic();
						activityCommentPic.setPicId(pic.getId());
						activityCommentPic.setActivityComment(activityComment);
						picSet.add(activityCommentPic);
					} catch (BizException e) {
						LOG.error(e.getMessage());
					}
				}
				activityComment.setPics(picSet);
			}

			ActivityComment targetActivityComment = activityCommentService.add(activityComment);
			if (targetActivityComment == null) {
				RetManager.getInstance().setRetError(json, RetManager.common_comment_fail);
			} else {
				RetManager.getInstance().setRetSuccess(json, RetManager.common_comment_success);
			}
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/my_activities", method = RequestMethod.GET)
	public @ResponseBody
	Object myActivities(String client_key, String user_id, String access_token, String page, String size, String activity_filter) throws Exception {
		JSONObject json = new JSONObject();
		try {
			Pager pager = new Pager();
			if (StringUtils.isNotBlank(page)) {
				pager.setCurrentPage(Integer.parseInt(page));
			}
			if (StringUtils.isNotBlank(size)) {
				pager.setPageSize(Integer.parseInt(size));
			}
			List<Activity> activities = activityUserService.getMyActivities(user_id, activity_filter, pager);
			JSONArray jsonArray = new JSONArray();
			for (Activity activity : activities) {
				ContentJSONObject jsonObject = new ContentJSONObject();
				
				ActivityUser actUser = activityUserService.getByUserIdAndActivity(user_id,activity.getId());
				jsonObject.put("activity_id", activity.getId());
				jsonObject.put("cover_pic", activity.getCoverPicId());
				jsonObject.put("title", activity.getTitle());
				jsonObject.put("begin_time", activity.getBeginTime());
				jsonObject.put("type_name", activity.getTypeName());
				jsonObject.put("organization_name", activity.getOrganizationName());
				jsonObject.put("adult_price", activity.getAdultPrice());
				jsonObject.put("child_price", activity.getChildPrice());
				jsonObject.put("adult_count",String.valueOf(actUser.getAdultCount()));
				jsonObject.put("child_count",String.valueOf(actUser.getChildCount()));
				jsonObject.put("ages", activity.getAges());
				jsonObject.put("activity_address", activity.getActivityAddress());
				jsonObject.put("activity_address_lon", activity.getActivityAddressLon());
				jsonObject.put("activity_address_lat", activity.getActivityAddressLat());
				jsonArray.add(jsonObject.getContent());
			}
			json.put("content", jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/my_activity_comments", method = RequestMethod.GET)
	public @ResponseBody
	Object myActivityComments(String client_key, String user_id, String access_token, String page, String size) throws Exception {
		JSONObject json = new JSONObject();
		try {
			Pager pager = new Pager();
			if (StringUtils.isNotBlank(page)) {
				pager.setCurrentPage(Integer.parseInt(page));
			}
			if (StringUtils.isNotBlank(size)) {
				pager.setPageSize(Integer.parseInt(size));
			}
			List<ActivityComment> activityComments = activityCommentService.getMyComments(user_id, pager);
			JSONArray contentArray = new JSONArray();
			if (!activityComments.isEmpty()) {
				for (ActivityComment activityComment : activityComments) {
					ContentJSONObject jsonObject = new ContentJSONObject();
					jsonObject.put("comment_id", activityComment.getId());
					jsonObject.put("head_pic", activityComment.getUser() == null ? "" : activityComment.getUser().getHeadPicId());
					jsonObject.put("name", activityComment.getUser() == null ? "" : activityComment.getUser().getName());
					jsonObject.put("time", activityComment.getCreateTime());
					jsonObject.put("content", activityComment.getContent());
					jsonObject.put("activity_id", activityComment.getActivity() == null ? "" : activityComment.getActivity().getId());
					jsonObject.put("activity_name", activityComment.getActivity() == null ? "" : activityComment.getActivity().getTitle());
					JSONArray picArray = new JSONArray();
					Set<ActivityCommentPic> activityCommentPics = activityComment.getPics();
					if (!activityCommentPics.isEmpty()) {
						for (ActivityCommentPic activityCommentPic : activityCommentPics) {
							picArray.add(activityCommentPic.getPicId());
						}
					}
					jsonObject.put("pics", picArray);
					contentArray.add(jsonObject.getContent());
				}
			}
			json.put("content", contentArray);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

	@Validate(retCode = RetManager.common_delete_error)
	@RequestMapping(value = "/delete_activity_comment", method = RequestMethod.POST)
	public @ResponseBody
	Object deleteActivityComment(String client_key, String user_id, String access_token, String comment_id) throws Exception {
		JSONObject json = new JSONObject();
		try {
			ActivityComment activityComment = activityCommentService.get(comment_id);
			if (activityComment != null && activityComment.getUserId().equals(user_id)) {
				activityCommentService.delete(comment_id);
				RetManager.getInstance().setRetSuccess(json, RetManager.common_delete_success);
			} else {
				RetManager.getInstance().setRetError(json, RetManager.common_delete_fail);
			}

		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/activity_filter_params", method = RequestMethod.GET)
	public @ResponseBody
	Object activityFilterParams(String client_key) throws Exception {
		JSONObject json = new JSONObject();
		try {
			ContentJSONObject contentJSONObject = new ContentJSONObject();
			List<ActivityType> list = activityTypeService.getAllTypes();

			JSONArray types = new JSONArray();
			for (ActivityType activityType : list) {
				ContentJSONObject jsonObject = new ContentJSONObject();
				jsonObject.put("type_id", activityType.getId());
				jsonObject.put("type_name", activityType.getTypeName());
				jsonObject.addSelf2Array(types);
			}
			contentJSONObject.put("types", types);
			JSONArray ages = new JSONArray();
			ages.add("1");
			ages.add("2");
			ages.add("3");
			ages.add("4");
			ages.add("5");
			ages.add("6");
			ages.add("7");
			ages.add("8");
			ages.add("9");
			ages.add("10");
			ages.add("11");
			ages.add("12");
			contentJSONObject.put("ages", ages);
			contentJSONObject.addSelf2Json(json);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

	/**
	 * 支付宝回调
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Validate
	@RequestMapping(value = "/pay_ali_callback", method = RequestMethod.POST)
	public @ResponseBody
	Object payAliCallback(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		LOG.error("===========【ActivityControll】收到支付宝回调===========");
		try {
			this.activityUserService.payCallback(request, response, PayUtils.PAY_TYPE_ALI);
			
		} catch (BizException e) {
			RetManager.getInstance().setRetSuccess(json, RetManager.common_error);
			LOG.error(e.getMessage());
		}
		return json;
	}

	/**
	 * 微信回调
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Validate
	@RequestMapping(value = "/pay_wx_callback", method = RequestMethod.POST)
	public @ResponseBody
	Object payWxCallback(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		LOG.error("===========【ActivityControll】收到微信回调===========");
		try {
			this.activityUserService.payCallback(request, response, PayUtils.PAY_TYPE_WX);
		} catch (BizException e) {
			RetManager.getInstance().setRetSuccess(json, RetManager.common_error);
			LOG.error(e.getMessage());
		}
		return json;
	}

	
	@Validate
	@RequestMapping(value = "/mengzhu_index",method=RequestMethod.GET)
	public @ResponseBody Object index(String client_key){
		
		JSONObject json = new JSONObject();
		//获取banner
		try {
			JSONObject content = new JSONObject();
			Pager pager = new Pager();
			Banner target = new Banner();
			List<Banner> banners = this.bannerService.showList(target, pager, null);
			JSONArray bannerJsonArray = new JSONArray();
			for (Banner banner : banners) {
				JSONObject bannerJson = new JSONObject();
				bannerJson.put("banner_id", banner.getId());
				bannerJson.put("activity_id", banner.getActivityId()==null?StringUtils.EMPTY:ObjUtils.getOpVal(banner.getActivityId()));
				bannerJson.put("banner_pic_id", banner.getPicId()==null?StringUtils.EMPTY:ObjUtils.getOpVal(banner.getPicId()));
				bannerJson.put("url","http://120.76.103.214:8080/FutureBabyApp/banner_detail_page/"+banner.getId());
				bannerJsonArray.add(bannerJson);
			}
			content.put("banner", bannerJsonArray);
			//获取活动
			
			
			
			
			
			json.put("content", content);
		} catch (BizException e) {
			json.put("ret",ErrorConstants.ERROR);
			json.put("error_code", ErrorConstants.SEARCH_ERROR);
			LOG.error(e.getMessage());
		}
		return json;
	}
	
	/**
	 * banner详情页面
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/banner_detail_page/{id}",method=RequestMethod.GET)
	public String bannerDetailPage(@PathVariable("id") String id,Model model){
		try {
			Banner banner = this.bannerService.get(id);
			model.addAttribute("banner",banner);
		} catch (BizException e) {
			LOG.error(e.getMessage());
		}
		return "bannerDetailPage";
	}
	
	
}
